package com.tangyuan.oss.service.impl;

import com.tangyuan.oss.adapter.StorageAdapter;
import com.tangyuan.oss.service.FileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author: TangYuan
 * @Date: 2024/6/3 16:21
 * @描述:
 */
@Service
public class FileServiceImpl implements FileService {
    
    private final StorageAdapter storageAdapter;
    
    public FileServiceImpl(StorageAdapter storageAdapter) {
        this.storageAdapter = storageAdapter;
    }
    
    /**
     * 列出所有桶
     * @return {@link List< String>}
     * @Date: 2024/6/3 下午4:22
     */
    public List<String> getAllBucket() {
        return storageAdapter.getAllBucket();
    }
    
    /**
     * 获取文件路径
     * @param bucketName 桶名
     * @param fileName   文件名
     * @return {@link String}
     * @Date: 2024/6/3 下午4:22
     */
    public String getUrl(String bucketName, String fileName) {
        return storageAdapter.getUrl(bucketName, fileName);
    }
    
    /**
     * 上传文件
     * @param uploadFile 文件流
     * @param bucketName 桶名
     * @param fileName   文件名
     * @return {@link String}
     * @Date: 2024/6/3 下午4:22
     */
    public String uploadFile(MultipartFile uploadFile, String bucketName, String fileName) {
        storageAdapter.uploadFile(uploadFile, bucketName, fileName);
        fileName = fileName + "/" + uploadFile.getOriginalFilename();
        return storageAdapter.getUrl(bucketName, fileName);
    }
}
